#ifndef _DSWAUDIO_CODEC_H
#define _DSWAUDIO_CODEC_H
#include <libbase/dswmemlistbase.h>
#include <libdswmemory/dswmemory.h>
#include <libdswav/g711codec.h>

typedef struct{
	char* pMsgSound;
	int nMsgSoundLen;
} ST_MSG_SOUND_T, *LPST_MSG_SOUND_T;

typedef std::list<LPST_MSG_SOUND_T> LIST_MSG_SOUND_T, *LPLIST_MSG_SOUND_T;

class CDswaudio_codec : public CDswmemlistbase
{
public:
	CDswaudio_codec(LPST_ENV_T pEnv, int nMemSize);
	virtual ~CDswaudio_codec();
private:
	bool GetAudioFrame(LPST_AUDIO_FRAME_T pFrame);
	void PutAudioFrame(LPST_AUDIO_FRAME_T pFrame);
	void InitEncList();
	void UnInitEncList();
	void InitDecList();
	void UnInitDecList();

	void StartEncThread();
	void StopEncThread();
	void StartDecThread();
	void StopDecThread();

	static void* cbEncThreadFunc(void* pParam){((CDswaudio_codec*)pParam)->EncThreadFunc();return NULL;};
	void EncThreadFunc();
	static void* cbDecThreadFunc(void* pParam){((CDswaudio_codec*)pParam)->DecThreadFunc();return NULL;};
	void DecThreadFunc();

	void EncPCMData(char* pInData, int nInLen, char* pOutData, int &nOutLen);
	void DecPCMData(char* pInData, int nInLen, char* pOutData, int &nOutLen);
	bool GetMsgSound(char* pOutData, int &nOutLen);
	void SetMsgSound(EN_MSG_SOUND nType);
protected:
	virtual bool DumpFrame(void* pSrc, void* pDest);
	virtual int OnMessage(EN_MSG_TYPE nMsgType, void * pParam);
private:
	CDswmemory* m_pMemory;
	pthread_t m_nAudioEncThread, m_nAudioDecThread;
	ST_AUDIO_PARAM m_nAudioParam;

	void* m_grab_groupfd;
	void* m_grab_bindfd[MAX_CHANNEL_LOCAL];
	void* m_audio_grab_object[MAX_CHANNEL_LOCAL];
	void* m_audio_encode_object[MAX_CHANNEL_LOCAL];

	void *m_render_groupfd;
	void *m_render_bindfd;
	void *m_render_file_object;
	void *m_audio_render_object;

	ST_LIST_MEM_T m_nUnUsed_Dec, m_nUsed_Dec;

	CG711Codec m_nAudioCodec;

	LIST_MSG_SOUND_T m_nMsgSndFree, m_nMsgSnd;
	pthread_mutex_t m_nMsgSndLock;

	int m_nMaxAudioChannel;
};

#endif

